﻿#region

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
using BusinessObjects;
using DataAccessLayer;
using Twilio;

#endregion

namespace BusinessLogicLayer
{
    public static class BookingBLL
    {
        /// <summary>
        ///     Get all bookings by booking status
        /// </summary>
        /// <param name="status">Represent's the booking status (pending / cancelled / rejected / approved / settled)</param>
        /// <returns>Bookings</returns>
        public static List<Booking> GetAllBookings(string status)
        {
            return BookingDAL.GetAllBookings(status);
        }

        /// <summary>
        ///     Get all bookings by booking Id
        /// </summary>
        /// <param name="bookingId">Represent's the booking Id</param>
        /// <returns>Booking</returns>
        public static Booking GetBooking(int bookingId)
        {
            return BookingDAL.GetBooking(bookingId);
        }

        /// <summary>
        ///     Get all bookings
        /// </summary>
        /// <returns>Bookings</returns>
        public static List<Booking> GetAllBookings()
        {
            return BookingDAL.GetAllBookings();
        }

        /// <summary>
        ///     Reset booking confirmation (Roll back to pending)
        /// </summary>
        /// <param name="bookingId">Represent's the booking Id</param>
        public static void ResetConfirmation(int bookingId)
        {
            BookingDAL.ResetConfirmation(bookingId);
        }

        /// <summary>
        ///     Get all booking type by booking id
        /// </summary>
        /// <param name="bookingId">Represent's the booking Id</param>
        /// <returns>Booking type</returns>
        public static string GetBookingType(int bookingId)
        {
            return BookingDAL.GetBookingType(bookingId);
        }

        /// <summary>
        ///     Change booking status by booking id
        /// </summary>
        /// <param name="bookingId">Represent's the booking Id</param>
        /// <param name="status">Represent's the booking status</param>
        public static void ChangeStatus(int bookingId, string status)
        {
            BookingDAL.ChangeStatus(bookingId, status);
        }

        /// <summary>
        ///     Confirm the booking
        /// </summary>
        /// <param name="bookingId">Represent's the booking Id</param>
        /// <param name="supervisorId">Represent's the supervisor's Id</param>
        /// <param name="approxEndDate">
        ///     Represent's the approximate booking end date and time (or travel duration or vehicle return
        ///     time)
        /// </param>
        public static void ApproveBooking(int bookingId, int supervisorId, DateTime approxEndDate)
        {
            Booking booking = new Booking
            {
                BookingId = bookingId,
                SupervisorId = supervisorId,
                ApproximateEndDateTime = approxEndDate
            };

            BookingDAL.ApproveBooking(booking);
        }

        /// <summary>
        ///     Settle or complete booking
        /// </summary>
        /// <param name="bookingId">Represent's the booking Id</param>
        /// <param name="travelledDistance">Represent's the travelled distance in km</param>
        /// <param name="deliveredDateTime">Represent's the vehicle delivered date and time</param>
        public static void SettleBooking(int bookingId, int travelledDistance, DateTime deliveredDateTime)
        {
            Booking booking = new Booking
            {
                BookingId = bookingId,
                TravelledDistance = travelledDistance,
                DeliveredDateTime = deliveredDateTime
            };

            BookingDAL.SettleBooking(booking);
        }

        public static int GetNofificationCount(int customerId)
        {
            return BookingDAL.GetNofificationCount(customerId);
        }

        public static List<Booking> GetNofification(int customerId)
        {
            return BookingDAL.GetNofification(customerId);
        }

        public static void ClearNofification(int bookingId)
        {
            BookingDAL.ClearNofification(bookingId);
        }

        public static void SendConfirmationMail(int bookingId)
        {
            Booking booking;
            string destination;
            List<Booking_Vehicle_Driver> bvds;

            if (GetBookingType(bookingId) == "PICK")
            {
                PickBooking pick = PickBookingBLL.GetBooking(bookingId);
                destination = pick.Destination;
                bvds = pick.VehicleDrivers;
                booking = pick;
            }
            else
            {
                PickAndDropBooking pickAndDrop = PickAndDropBookingBLL.GetBooking(bookingId);
                destination = pickAndDrop.MidPoint;
                bvds = pickAndDrop.VehicleDrivers;
                booking = pickAndDrop;
            }

            int staffId = booking.SupervisorId ?? 0;
            Staff supervisor = StaffBLL.GetStaffById(staffId);
            string customerName = CustomerBLL.GetCustomerName(booking.CustomerId);
            Customer customer = CustomerBLL.GetAllCustomers().Single(c => c.CustomerId == booking.CustomerId);
            string eMail = customer.EMail;

            string content =
                "Dear " + customerName +
                "<br/><br/>Please find your booking details. If you have any doubts please don't hesitate to contact us.<br/><br/>" +
                @"<table class='detailsView' cellspacing='0' cellpadding='4' id='ContentPlaceHolder1_dvPick'
    style='color: #333333; border-collapse: collapse;'>
    <tr style='color: White; background-color: #5D7B9D; font-weight: bold;'>
        <td colspan='2' style='padding: 0.5em; text-align: center; border-top-left-radius: 0.5em;
            border-top-right-radius: 0.5em;'>
            Booking details
        </td>
    </tr>
    <tr style='color: #333333; background-color: #F7F6F3;'>
        <td style='background-color: #E9ECF1; font-weight: bold; padding: 0.5em;'>
            Booking Id
        </td>
        <td style='padding: 0.5em;'>" +
                bookingId +
                @"</td>
    </tr>
    <tr style='color: #284775; background-color: White;'>
        <td style='background-color: #E9ECF1; padding: 0.5em; font-weight: bold;'>
            Customer
        </td>
        <td style='padding: 0.5em;'>" +
                customerName +
                @"</td>
    </tr>
    <tr style='color: #284775; background-color: White;'>
        <td style='background-color: #E9ECF1; font-weight: bold; padding: 0.5em;'>
            Pickup date &amp; time
        </td>
        <td style='padding: 0.5em;'> " +
                booking.OriginDateTime +
                @"</td>
    </tr>
    <tr style='color: #333333; background-color: #F7F6F3;'>
        <td style='background-color: #E9ECF1; font-weight: bold; padding: 0.5em;'>
            From
        </td>
        <td style='padding: 0.5em;'>
            <div>
                <table class='innerDetailsView' cellspacing='0' cellpadding='4' id='ContentPlaceHolder1_dvPick_DetailsView2'
                    style='color: #333333; height: 50px; width: 125px; border-collapse: collapse;
                    width: 100%;'>
                    <tr style='color: #333333; background-color: #F7F6F3;'>
                        <td style='background-color: #E9ECF1; padding: 0.5em; font-weight: bold;'>
                            No / Name
                        </td>
                        <td>" +
                booking.OriginLocation.HomeNumberOrName +
                @"</td>
                    </tr>
                    <tr style='color: #284775; background-color: White;'>
                        <td style='background-color: #E9ECF1; padding: 0.5em; font-weight: bold;'>
                            Street
                        </td>
                        <td>" +
                booking.OriginLocation.Street +
                @"</td>
                    </tr>
                    <tr style='color: #333333; background-color: #F7F6F3;'>
                        <td style='background-color: #E9ECF1; padding: 0.5em; font-weight: bold;'>
                            City
                        </td>
                        <td>" +
                booking.OriginLocation.City +
                @"</td>
                    </tr>
                    <tr style='color: #284775; background-color: White;'>
                        <td style='background-color: #E9ECF1; padding: 0.5em; font-weight: bold;'>
                            District
                        </td>
                        <td>" +
                DistrictBLL.GetDistrict(Convert.ToByte(booking.OriginLocation.DistrictId)) +
                @"</td>
                    </tr>
                </table>
            </div>
        </td>
    </tr>
    <tr style='color: #284775; background-color: White;'>
        <td style='background-color: #E9ECF1; font-weight: bold; padding: 0.5em;'>
            To
        </td>
        <td>" +
                destination +
                @"</td>
    </tr>
    <tr style='color: #333333; background-color: #F7F6F3;'>
        <td style='background-color: #E9ECF1; font-weight: bold; padding: 0.5em;'>
            Type
        </td>
        <td>" +
                booking.Type +
                @"</td>
    </tr>
    <tr style='color: #333333; background-color: #F7F6F3;'>
        <td style='background-color: #E9ECF1; font-weight: bold; padding: 0.5em;'>
            Supervisor
        </td>
        <td style='background: white'>
            <table style='width: 100%;'>
                <tr>
                    <th style='text-align: left; color: #333333; padding: 0.5em; background-color: #E9ECF1;'>
                        Name
                    </th>
                    <td style='text-align: left; padding: 0.5em;'>" +
                supervisor.FirstName +
                @"</td>
                </tr>
                <tr>
                    <th style='text-align: left; width: 6em; color: #333333; padding: 0.5em; background-color: #E9ECF1;'>
                        Contact no
                    </th>
                    <td style='text-align: left; background-color: #F7F6F3; padding: 0.5em;'>" +
                supervisor.TelephoneNo +
                @"</td>
                </tr>
            </table>
        </td>
    </tr>
    <tr style='color: #284775; background-color: White;'>
        <td style='background-color: #E9ECF1; font-weight: bold; padding: 0.5em;'>
            Vehicle(s)
        </td>
        <td>
            <div>
                <table cellspacing='0' cellpadding='4' id='ContentPlaceHolder1_dvPick_GridView1'
                    style='color: #333333; border-collapse: collapse;'>
                    <tr style='color: White; background-color: #5D7B9D; font-weight: bold;'>
                        <th scope='col' style='padding: 0.5em;'>
                            Vehicle reg no
                        </th>
                        <th scope='col' style='padding: 0.5em;'>
                            Type
                        </th>
                        <th scope='col' style='padding: 0.5em;'>
                            Driver
                        </th>
                        <th scope='col' style='padding: 0.5em;'>
                            Contact no
                        </th>
                    </tr>";

            foreach (var bookingVehicleDriver in bvds)
            {
                Staff driver = StaffBLL.GetStaffById(bookingVehicleDriver.DriverId);
                Vehicle vehicle = VehicleBLL.GetVehiclesByRegNo(bookingVehicleDriver.VehicleRegistrationNo);
                content += @"<tr style='color: #333333; background-color: #F7F6F3;'>
                                <td style='padding: 0.5em;'>" +
                           bookingVehicleDriver.VehicleRegistrationNo +
                           @"</td>
                                <td style='padding: 0.5em;'>" +
                           vehicle.Type +
                           @"</td>
                                <td style='padding: 0.5em;'>" +
                           driver.FirstName +
                           @"</td>
                                <td style='padding: 0.5em;'>" +
                           driver.TelephoneNo +
                           @"</td>
                            </tr>";
            }
            content +=
                @"</table>
            </div>
        </td>
    </tr>
</table>
<br/><br/><div style='text-align:center;'>Thank you</div>
<br/><br/>
Sincerly<br/>
<b>Fast Track Cab Service Online Support Team</b>
";

            MailMessage mail = new MailMessage("cabservicefasttrack@gmail.com", eMail,
                "Booking confirmation - Fast Track Cab Services",
                content)
            {
                IsBodyHtml = true
            };

            new SmtpClient().Send(mail);
            SendSMS(customer.TelephoneNo, supervisor, bvds);
        }

        private static void SendSMS(string toPhone, Staff supervisor, List<Booking_Vehicle_Driver> bvds)
        {
            string phone = "+94" + toPhone.Substring(1).Replace("-", "");

            const string accountSid = "AC5d7aa3f501cca0d5c791d60d7f075d2a";
            const string authToken = "d6e995cbab9bef71ac236e848276c79a";
            var twilio = new TwilioRestClient(accountSid, authToken);

            string message = "Your booking details are below:"
                             + "\n\nSupervisor:"
                             + "\n\nName: " + supervisor.FirstName
                             + "\nTelephone: " + supervisor.TelephoneNo
                             + "\n\n\nDriver(s) & Vehicle(s) details:";

            foreach (Booking_Vehicle_Driver bvd in bvds)
            {
                Staff driver = StaffBLL.GetStaffById(bvd.DriverId);
                Vehicle vehicle = VehicleBLL.GetVehiclesByRegNo(bvd.VehicleRegistrationNo);

                message += "\n\nName: " + driver.FirstName
                           + "\nTelephone: " + driver.TelephoneNo
                           + "\nVehicle: " + bvd.VehicleRegistrationNo
                           + "\nType: " + vehicle.Type;
            }

            message += "\n\nFor more information about the booking, please login to your account or check you email!"
                       + "\n If you have any doubts to clarify please do not hesitate to contact us."
                       + "\n\nSincerly"
                       + "\nFast Track Cab Service Customer Care Team"
                       + "\ncabservicefasttrack@gmail.com";

            twilio.SendMessage("+12569608066", phone, ".\n\n\n" + message);
        }
    }
}